﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using G.SL.Controls;

namespace G.APITest.Views
{
    public partial class WelcomePage : Page
    {
        public WelcomePage()
        {
            InitializeComponent();
            MenuItem item1 = new MenuItem() { Name = "帮助" };
            item1.Items.Add(new MenuItem() { Name = "检查更新", Command = new SL.MVVM.CallBackCommand(CheckUpadate) });
            PageMenuService.GetMenu(this).Add(item1);
        }

        private void CheckUpadate(object p)
        {
            if (App.Current.IsRunningOutOfBrowser)
            {
                App.Current.CheckAndDownloadUpdateCompleted -= App_CheckAndDownloadUpdateCompleted;
                App.Current.CheckAndDownloadUpdateCompleted += App_CheckAndDownloadUpdateCompleted;
                App.Current.CheckAndDownloadUpdateAsync();
                gridprogress.Visibility = System.Windows.Visibility.Visible;
            }
        }

        void App_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
        {
            gridprogress.Visibility = System.Windows.Visibility.Collapsed;
            if (e.Error != null)
            {
                if (e.Error is PlatformNotSupportedException)
                    MessageBox.Show("请升级您的Silverlight插件！");
                else
                    MessageBox.Show("更新出错: " + e.Error.Message);
                return;
            }
            if (e.UpdateAvailable)
            {
                MessageBox.Show("更新完毕！请重启程序！");
            }
            else
                MessageBox.Show("无更新！");
        }

        // 当用户导航到此页面时执行。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

    }
}
